package com.jvyou.driver.client;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jvyou.driver.domain.dto.SparkRequestDto;
import com.jvyou.driver.domain.dto.SparkResponseDto;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * 讯飞星火认知大模型客户端
 *
 * @author 橘柚
 * @since 2024/12/20 12:13
 */

@Service
public class SparkServiceClient {

    private final ObjectMapper objectMapper = new ObjectMapper();

    public SparkResponseDto chat(SparkRequestDto sparkRequestDto){
        RestTemplate restTemplate = new RestTemplate();

        String url = "https://spark-api-open.xf-yun.com/v1/chat/completions";
        HttpHeaders headers = new HttpHeaders();
        headers.set("Authorization", "Bearer fTMZcgpapAbZetwizBSn:UZedeOMeLZrcxKXtUqCa");
        headers.setContentType(MediaType.APPLICATION_JSON);

        String requestBody = null;
        try {
            requestBody = objectMapper.writeValueAsString(sparkRequestDto);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
        HttpEntity<String> request = new HttpEntity<>(requestBody, headers);
        ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, request, String.class);
        return null;
    }

}
